import { Controller, Post, Body, Headers, Get, Query } from '@nestjs/common';
import { OrderService } from './order.service';
import {
  PayForCouponDto,
  PayForTaskDto,
  PayForTokenDto,
  PayForVipDto,
  RefundDto,
} from './dto/create-order.dto';
import { Public } from '@/decorator/public';
import { ApiOperation } from '@nestjs/swagger';
import { WepayCallbackDto } from '../wepay/dto/wepay.callback.dto';
import { WepayService } from '../wepay/wepay.service';

@Controller('order')
export class OrderController {
  constructor(
    private readonly orderService: OrderService,
    private readonly wepayService: WepayService,
  ) { }

  // @Post('payfor/token/mock')
  // payforTokenMock(@Body() payForTokenDto: PayForTokenDto) {
  //   return this.orderService.createTokenOrderMock(payForTokenDto);
  // }

  @Post('payfor/vip/mock')
  payforVipMock(@Body() payforVipDto: PayForVipDto) {
    return this.orderService.createVipOrderMock(payforVipDto);
  }

  @Post('payfor/token')
  payforToken(@Body() payForTokenDto: PayForTokenDto) {
    return this.orderService.createTokenOrder(payForTokenDto);
  }

  @Post('payfor/vip')
  payforVip(@Body() payforVipDto: PayForVipDto) {
    return this.orderService.createVipOrder(payforVipDto);
  }

  @Post('refund')
  async Refunds(@Body() refundDto: RefundDto) {
    return await this.orderService.refund(refundDto);
  }

  @Public()
  @Post('callback/payfor/token')
  @ApiOperation({ summary: 'token充值回调接口' })
  async PayForTokenSuccess(
    @Headers() headers: Record<string, string>,
    @Body() wepayCallbackDto: WepayCallbackDto,
  ): Promise<void> {
    // 签名校验
    await this.wepayService.verifySign(headers, wepayCallbackDto);
    // 解密支付详情
    const detail = this.wepayService.decipherGcm(wepayCallbackDto);
    // 处理支付成功逻辑
    const orderId = detail.out_trade_no;
    await this.orderService.payForTokenSuccess(orderId);
  }

  @Public()
  @Post('callback/payfor/vip')
  @ApiOperation({ summary: '会员购买回调接口' })
  async PayForVipSuccess(
    @Headers() headers: Record<string, string>,
    @Body() wepayCallbackDto: WepayCallbackDto,
  ): Promise<void> {
    // 签名校验
    await this.wepayService.verifySign(headers, wepayCallbackDto);
    // 解密支付详情
    const detail = this.wepayService.decipherGcm(wepayCallbackDto);
    // 处理支付成功逻辑
    const orderId = detail.out_trade_no;
    await this.orderService.payForVipSuccess(orderId);
  }

  @Public()
  @Post('callback/refund')
  @ApiOperation({ summary: '微信退款回调接口' })
  async RefundsCallback(
    @Headers() headers: Record<string, string>,
    @Body() wepayCallbackDto: WepayCallbackDto,
  ): Promise<void> {
    // 签名校验
    await this.wepayService.verifySign(headers, wepayCallbackDto);
    // 解密退款详情
    const detail = this.wepayService.decipherGcm(wepayCallbackDto);
    // 处理退款成功逻辑
    const refundId = detail.out_refund_no;
    await this.orderService.refundSuccess(refundId);
  }

  @Post('create')
  createOrder(@Body() payForTaskDto: PayForTaskDto) {
    return this.orderService.createOrder(payForTaskDto);
  }

  @Post('get-prepay-id')
  getPrepayId(@Body() payForCouponDto: PayForCouponDto) {
    return this.orderService.getPrepayId(payForCouponDto);
  }

  @Get('detail')
  detail(@Query('id') id: string) {
    return this.orderService.Detail(id);
  }

  // 查询订单列表
  @Get('list')
  list(@Query('id') id: string) {
    return this.orderService.List(id);
  }

  // 管理员查询订单列表
  @Get('list-gly')
  listByGly(@Query('orderType') orderType: string) {
    return this.orderService.ListByGly(orderType);
  }

  // 管理员查询今日待完成列表
  @Get('list-gly-today')
  ListByGlyToday(@Query('orderType') orderType: string) {
    return this.orderService.ListByGlyToday(orderType);
  }

  @Get('list-gly-finish-time')
  listByGlyAndFinishTime(@Query('orderType') orderType: string) {
    return this.orderService.ListByGlyAndFinishTime(orderType);
  }

  // 管理员完成任务
  @Get('finish-task')
  finishTask(@Query('userId') userId: string, @Query('orderId') orderId: string) {
    return this.orderService.FinishTask(userId, orderId);
  }

  // @Get()
  // findAll() {
  //   return this.orderService.findAll();
  // }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.orderService.findOne(+id);
  // }

  // @Patch(':id')
  // update(@Param('id') id: string, @Body() updateOrderDto: UpdateOrderDto) {
  //   return this.orderService.update(+id, updateOrderDto);
  // }

  // @Delete(':id')
  // remove(@Param('id') id: string) {
  //   return this.orderService.remove(+id);
  // }
}
